package com.js.service.bot.response;

import com.js.common.annotation.ResponseClass;
import com.js.common.annotation.ResponseCommand;
import com.js.common.core.BotHandler;
import com.js.domian.entity.CommandEntity;
import com.js.service.bot.core.CommandService;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Scope("prototype")
@ResponseClass("系统")
public class CommonService extends BotHandler {

    @Resource
    private CommandService commandService;

    @ResponseCommand(
            value = "#所有(指令|命令|表情)",
            name = "所有指令列表"
    )
    public MessageChain list(Long userId, Long targetId, String command, MessageChain messageChain) {
        MessageChainBuilder result = new MessageChainBuilder();
        CommandEntity param = new CommandEntity();
        param.setQq(userId);
        param.setQun(targetId);
        List<CommandEntity> list = commandService.getCommandList(param);
        for (int i = 0; i < list.size(); i++) {
            CommandEntity entity = list.get(i);
            result.append(entity.getCommand());
            result.append(newline());
        }
        return result.build();
    }

    @ResponseCommand(
            value = ".+",
            name = "自定义指令"
    )
    public MessageChain response(Long userId, Long targetId, String command, MessageChain messageChain) {
        String message = messageChain.contentToString();
        CommandEntity param = new CommandEntity();
        param.setQq(userId);
        param.setQun(targetId);
        param.setCommand(message);
        List<CommandEntity> list = commandService.getCommand(param);
        if(list.size() == 0){
            return null;
        }
        for (CommandEntity entity : list) {
            send(userId, targetId, MessageChain.deserializeFromJsonString(entity.getResponse()));
        }
        return null;
    }

    @ResponseCommand(
            value = "#添加(.+)",
            name = "添加指令",
            reg = "#添加"
    )
    public MessageChain add(Long userId, Long targetId, String command, MessageChain messageChain) {
        Map<String, Object> map = new HashMap<>();
        map.put("command", getContent().get(0));
        setCache("addCommand", map);
        MessageChainBuilder result = new MessageChainBuilder();
        result.append("请发送添加内容");
        return result.build();
    }

    public MessageChain addCommand(Long userId, Long targetId, String command, MessageChain messageChain){
        if(!containCache("addCommand")){
            return null;
        }
        Map<String, Object> map = getCache("addCommand");
        CommandEntity entity = new CommandEntity();
        entity.setCommand(map.get("command").toString());
        entity.setResponse(MessageChain.serializeToJsonString(messageChain));
        entity.setQq(userId);
        entity.setQun(targetId);
        boolean success = commandService.addCommand(entity);
        MessageChainBuilder result = new MessageChainBuilder();
        if(success){
            result.append("添加成功");
        }else{
            result.append("添加失败");
        }
        removeCache("addCommand");
        return result.build();
    }

    @ResponseCommand(
            value = "#删除(.+)",
            name = "删除指令",
            reg = "#删除"
    )
    public MessageChain del(Long userId, Long targetId, String command, MessageChain messageChain) {
        MessageChainBuilder result = new MessageChainBuilder();
        CommandEntity param = new CommandEntity();
        param.setQq(userId);
        param.setQun(targetId);
        param.setCommand(getContent().get(0));
        List<CommandEntity> list = commandService.getCommand(param);
        if(list.size() == 0){
            result.append("未找到["+getContent().get(0)+"]");
            return result.build();
        }
        boolean success = commandService.deleteCommand(param);
        if(success){
            result.append("删除成功");
        }else{
            result.append("删除失败");
        }
        return result.build();
    }
}
